package com.tutu.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tutu.common.GlobalConstants;
import com.tutu.common.util.BeanUtils;
import com.tutu.user.dao.SysUserGroupPowerMapper;
import com.tutu.user.entity.SysUserGroupPower;
import com.tutu.user.entity.SysUserGroupPowerExample;
import com.tutu.user.entity.SysUserGroupPowerExample.Criteria;
import com.tutu.user.service.ISysUserGroupPowerService;

@Service
public class SysUserGroupPowerServiceImpl implements ISysUserGroupPowerService {

	@Resource
	private SysUserGroupPowerMapper sysUserGroupPowerMapper;
	
	@Override
	public void deleteSysUserGroupPower(SysUserGroupPower sysUserGroupPower) {
		SysUserGroupPowerExample sysUserGroupPowerExample = new SysUserGroupPowerExample();
		Criteria criteria = sysUserGroupPowerExample.createCriteria();
		if(StringUtils.isNotBlank(sysUserGroupPower.getGroupId()))
			criteria.andGroupIdEqualTo(sysUserGroupPower.getGroupId());
		this.sysUserGroupPowerMapper.deleteByExample(sysUserGroupPowerExample);
	}

	@Override
	public void saveSysUserGroupPowers(String groupId, List<String> menuIds) {
		SysUserGroupPower sysUserGroupPower = new SysUserGroupPower();
		sysUserGroupPower.setGroupId(groupId);
		deleteSysUserGroupPower(sysUserGroupPower);
		for(String menuId:menuIds){
			SysUserGroupPower temp = new SysUserGroupPower();
			temp.setGroupId(groupId);
			temp.setMenuId(menuId);
			temp.setAliveFlag(GlobalConstants.FLAG_ENABLE);
			temp.setCreator(GlobalConstants.SYSTEM_USER_ID);
			this.sysUserGroupPowerMapper.insert(temp);
		}
	}

	@Override
	public List<SysUserGroupPower> getSysUserGroupPowersByRoleId(String groupId)
			throws Exception {
		SysUserGroupPowerExample sysUserGroupPowerExample = new SysUserGroupPowerExample();
		Criteria criteria = sysUserGroupPowerExample.createCriteria();
		criteria.andGroupIdEqualTo(groupId);
		sysUserGroupPowerExample.setOrderByClause("CREATE_TIME DESC");
		return this.sysUserGroupPowerMapper.selectByExample(sysUserGroupPowerExample);
	}

}
